<template>
  <div class="goods-hot">
      <!--
       适配title
       type === 1 24小时热榜
       type === 2 周热榜
       type === 3 总热榜
    -->
    <h3>{{ TYPES[type] }}</h3>
    <!-- 商品区块 -->
    <RouterLink
      to="/"
      class="goods-item"
      v-for="i in goodList"
      :key="i.id"
    >
      <img :src="i.picture" alt="" />
      <p class="name ellipsis">{{ i.name }}</p>
      <p class="desc ellipsis">{{i.desc}}</p>
      <p class="price">&yen;{{i.price}}</p>
    </RouterLink>
  </div>
</template>
<script>
import { findHotGoodsAPI } from '@/api/good'
import { useRoute } from 'vue-router'
import { ref } from 'vue'
export default {
  name: 'GoodsHot',
  props: {
    // 作为一个业务上的判断条件来使用
    // 当成一个判断条件 做了俩个事儿 一个是用来作为获取不同接口数据的type参数
    // 作为判断title显示的key
    type: {
      type: String
    }
  },
  setup (props) {
    // 准备一个type的枚举 （映射关系）
    const TYPES = {
      1: '24小时热榜',
      2: '周热榜',
      3: '总热榜'
    }
    const goodList = ref([])
    const route = useRoute()
    async function loadGoodListFn () {
      const res = await findHotGoodsAPI({
        id: route.params.id,
        type: props.type // type需要父组件传入
      })
      //   console.log(res)
      goodList.value = res.result
    }
    loadGoodListFn()

    return {
      goodList,
      TYPES
    }
  }
}
</script>
<style scoped lang="less">
.goods-hot {
  h3 {
    height: 70px;
    background: @helpColor;
    color: #fff;
    font-size: 18px;
    line-height: 70px;
    padding-left: 25px;
    margin-bottom: 10px;
    font-weight: normal;
  }
  .goods-item {
    display: block;
    padding: 20px 30px;
    text-align: center;
    background: #fff;
    img {
      width: 160px;
      height: 160px;
    }
    p {
      padding-top: 10px;
    }
    .name {
      font-size: 16px;
    }
    .desc {
      color: #999;
      height: 29px;
    }
    .price {
      color: @priceColor;
      font-size: 20px;
    }
  }
}
</style>
